package com.young.mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Author: young
 * @Date: 2021-12-03 20:38
 * @Description: 基于Druid数据库连接池的工具类
 */
public class JdbcUtilsByDruid {
	private static DataSource ds;

	// 在static代码块完成ds初始化
	static {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream("src\\druid.properties"));
			ds = DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() throws SQLException {
		return ds.getConnection();
	}

	// 关闭连接：在数据库连接池技术中，是把Connection对象放回连接池
	public static void close(ResultSet resultSet, Statement statement, Connection connection){
		try {
			if (resultSet != null){
				resultSet.close();
			}
			if (statement != null){
				statement.close();
			}
			if (connection != null){
				connection.close();
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}
